<template>
  <div class="container">
    <div class="title" style=" text-align: left;">广告图片列表</div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="广告图片" prop="url">
              <upload-imgs :max-num="maxNum" ref="uploadEle"/>
            </el-form-item>
            <el-form-item label="图片优先级" prop="priority">
              <el-input clearable
                        type="number"
                        style="width: 300px"
                        placeholder="值越大优先级越高"
                        @mousewheel.native.prevent
                        @keyup.native="checkNum"
                        v-model.number="form.priority">
              </el-input>
            </el-form-item>
            <el-form-item label="是否启用" prop="state">
              <el-switch v-model="form.state"
                         :active-value="1"
                         active-text="启用"
                         inactive-text="禁用"
                         :inactive-value="0">
              </el-switch>
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')" :loading="loading">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { Advertising } from '@/model/advertising'
import UploadImgs from '@/component/base/upload-image'
export default {
  components: {
    UploadImgs,
  },
  data() {
    return {
      form: {
      },
      maxNum: 1,
      loading: false,
    }
  },
  methods: {
    async checkNum(){
      console.log(this)
    },
    // 上传图片之后，获取服务器返回的url地址
    async getValue() {
      const val = await this.$refs.uploadEle.getValue()
      if (val && val.length > 0) {
        this.form.url = val[0].display
      }
    },
    async submitForm(formName) {
      try {
        await this.getValue();
        this.loading = true
        const res = await Advertising.create(this.form)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          // 清空输入框，方便继续添加职位分类
          this.resetForm(formName)
        }
      } catch (error) {
        this.loading = false
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type='number'] {
    -moz-appearance: textfield;
  }

  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
